import cmdenums from "./cmdenums";
import drawcmdaction from "./drawcmdaction";
import editcmdaction from "./editcmdaction";
import {LGXEnums}  from "../../canvas/common/sysenum";


import globalConst from "../../canvas/common/globalConst";
var global = globalConst.GlobalConst;

class CMDMainControler {
    constructor() {
        this.curentAction = null;
        this.preCMD = "";
    }

    act2InputCMD = function (cmdinput, mainctl) {
        let actRst = null;
        const splitCh = " ";
        let list = cmdinput.split(splitCh);
        let type = list[0];
        let val = "";
        let firstBlackChIdx = cmdinput.indexOf(splitCh);
        if (firstBlackChIdx > 0) {
            val = cmdinput.substr(firstBlackChIdx + 1, cmdinput.length - firstBlackChIdx - 1);
        }
        actRst = this.actSysCMD(type, val, mainctl);
        if (!actRst) {
            if (!this.curentAction) {
                let subCtrlInfo = this.getSubControler(type);
                let newProcess = subCtrlInfo.newProcess;
                if (!this.curentAction || newProcess || this.preProcessType != type) {
                    let childControler = subCtrlInfo.childControler;
                    if (newProcess) {
                        this.curentAction = childControler.getCurentAction(type);
                    }
                }
            }

            if (this.curentAction) {
                actRst = this.curentAction.actCMD(type, val, mainctl);
                if (actRst && actRst.finish) {
                    this.curentAction = null;
                }
            }
        }
        return actRst;
    }

    actSysCMD(type, val, mainctl) {
        let flag = false;
        switch (type) {
            case "cls":
                flag = true;
                mainctl.clearData();
                break;
        }
        let actRst = null;
        if (flag) {
            let cmd = { type, value: "" };
            actRst = { cmdObj: cmd, nextTips: "", appendToPreCMD: "" };
        }
        return actRst;
    }

    act2ToolBtnCMD = function (toolcmd, mainctl) {
        const splitCh = "_";
        let list = toolcmd.split(splitCh);
        let type = list[0];
        if (type != "edit") {
            return null;
        }

        let subEditType = "";
        let firstBlackChIdx = toolcmd.indexOf(splitCh);
        if (firstBlackChIdx > 0) {
            subEditType = toolcmd.substr(firstBlackChIdx + 1, toolcmd.length - firstBlackChIdx - 1);
        }
        let subCtrlInfo = this.getSubControler(subEditType);
        let childControler = subCtrlInfo.childControler;
        let newProcess = subCtrlInfo.newProcess;
        if (newProcess) {
            this.curentAction = childControler.getCurentAction(subEditType);
        }
        return null;
    }

    getSubControler(type) {
        let childControler = null;
        let newProcess = false;
        switch (type) {
            case cmdenums.DrawCmdType.line:
            case cmdenums.DrawCmdType.circle:
            case cmdenums.DrawCmdType.rect:
            case cmdenums.DrawCmdType.arc:
            case cmdenums.DrawCmdType.ellipse:
            case cmdenums.DrawCmdType.ellipseArc:
                newProcess = true;
                this.preProcessType = type;
                childControler = new drawcmdaction.DrawCmdAction();
                break;

            case cmdenums.EditCmdType.trim:
            case cmdenums.EditCmdType.offset:
            case cmdenums.EditCmdType.rotate:
            case cmdenums.EditCmdType.mirror:
            case cmdenums.EditCmdType.extend:
            case cmdenums.EditCmdType.scale:
                newProcess = true;
                this.preProcessType = type;
                childControler = new editcmdaction.EditCmdAction;
                break;

        }
        return { childControler, newProcess };
    }

    actToNotifyMapActionProcess = function (mapEvt, mainctl) {
        let result = null;
        if (mapEvt.eventKind == LGXEnums.LGXEventKind.StartDrawNewTool) {
            let childControler = new drawcmdaction.DrawCmdAction();
            this.curentAction = childControler.getCurentActionByTool(mapEvt.drawTool);
        }
        else if (mapEvt.eventKind == LGXEnums.LGXEventKind.InSubEditMode) {
            let childControler = new editcmdaction.EditCmdAction();
            this.curentAction = childControler.getCurentAction(mapEvt.subEditMode);
        }

        if (this.curentAction) {
            result = this.curentAction.actCanvasEvt(mapEvt, mainctl);
            if (result && result.finish) {
                this.curentAction = null;
            }
        }
        return result;
    }

    actToSetDrawTool = function (drawTool, mainctl) {
        let result = null;
        let childControler = new drawcmdaction.DrawCmdAction();
        this.curentAction = childControler.getCurentActionByTool(drawTool);
        if (this.curentAction) {
            let cmdType = this.getCMDEnumsTypeByDrawTool(drawTool);
            result = this.curentAction.actCMD(cmdType, drawTool, mainctl);
            if (result) {
                result.appendToPreCMD = cmdType;
                if (result.finish) {
                    this.curentAction = null;
                }
            }
        }
        return result;
    }

    getCMDEnumsTypeByDrawTool = function (drawTool) {
        let cmdtype = "";
        let mainType = drawTool >> 16;
        switch (mainType) {
            case global.BaseUnitPolyline:
                cmdtype = cmdenums.DrawCmdType.line;
                break;

            case global.BaseUnitRectangle:
                cmdtype = cmdenums.DrawCmdType.rect;
                break;

            case global.BaseUnitCircle:
                cmdtype = cmdenums.DrawCmdType.circle;
                break;

            case global.BaseUnitArc:
                cmdtype = cmdenums.DrawCmdType.arc;
                break;

            case global.BaseUnitEllipse:
                cmdtype = cmdenums.DrawCmdType.ellipse;
                break;

            case global.BaseUnitEllipseArc:
                cmdtype = cmdenums.DrawCmdType.ellipseArc;
                break;
        }
        return cmdtype;
    }
}

export default { CMDMainControler }